package com.lezhi.socialgram.api.friend;

import com.lezhi.socialgram.enums.ResultEnum;
import com.lezhi.socialgram.exception.SocialGramException;
import com.lezhi.socialgram.mapper.sys.AccusationMapper;
import com.lezhi.socialgram.model.friend.Black;
import com.lezhi.socialgram.model.friend.MyLike;
import com.lezhi.socialgram.model.sys.Accusation;
import com.lezhi.socialgram.service.friend.FriendService;
import com.lezhi.socialgram.util.Result;
import com.lezhi.socialgram.util.ResultUtils;
import com.lezhi.socialgram.util.ValidUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by daizhen on 2017/4/11.
 */
@RestController
public class FriendApi {

    private static final Logger logger = LoggerFactory.getLogger(FriendApi.class);

    @Autowired
    FriendService friendService;

    @Autowired
    AccusationMapper accusationMapper;

    @PostMapping("/slide")
    public Result slide(@RequestBody @Valid MyLike myLike, BindingResult bindingResult) {
        ValidUtils.valid(logger, bindingResult);
        return friendService.slide(myLike);
    }

    @GetMapping("/friendList/{userId}/{lastFid}/{operation}")
    public Result friendList(@PathVariable("userId") Long userId,
                             @PathVariable("lastFid") Long lastFid,
                             @PathVariable("operation") Integer operation) {
        return friendService.myFriendList(userId, lastFid, operation);
    }

    @GetMapping("/myLikeList/{userId}/{lastFid}/{operation}")
    public Result myLikeList(@PathVariable("userId") Long userId,
                             @PathVariable("lastFid") Long lastFid,
                             @PathVariable("operation") Integer operation) {
        return friendService.myLikeList(userId, lastFid, operation);
    }

    @GetMapping("/likeMeList/{userId}/{lastFid}/{operation}")
    public Result likeMeList(@PathVariable("userId") Long userId,
                             @PathVariable("lastFid") Long lastFid,
                             @PathVariable("operation") Integer operation) {
        return friendService.likeMeList(userId, lastFid, operation);
    }

    @GetMapping("/myBlackList/{userId}/{lastFid}/{operation}")
    public Result myBlackList(@PathVariable("userId") Long userId,
                              @PathVariable("lastFid") Long lastFid,
                              @PathVariable("operation") Integer operation) {
        return friendService.myBlackList(userId, lastFid, operation);
    }

    @PostMapping("/pullIntoBlackList")
    public Result pullIntoBlackList(@RequestBody @Valid Black black, BindingResult bindingResult) {
        ValidUtils.valid(logger, bindingResult);
        return friendService.pullIntoBlackList(black);
    }

    @PostMapping("/accusation")
    public Result accusation(@RequestBody @Valid Accusation accusation, BindingResult bindingResult) {
        ValidUtils.valid(logger, bindingResult);
        try {
            accusationMapper.insertAccusation(accusation);
            return ResultUtils.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new SocialGramException(ResultEnum.INTERNAL_ERROR);
        }
    }
}
